#include "Log.h"
#include <Sock.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <Util.h>
using namespace muduo;
int main()
{
    // Socket sock;
    // sock.CreateClient(38080, "127.0.0.1");
    
    // for(int i=0;i<3;i++){
    //     std::string buffer = "nihao";
    //     debuglog<<"send nihao";
    //     ssize_t len = sock.Send(buffer.c_str(), buffer.size());
    //     char* buffer1[1024] = {0};
    //     sock.Recv(buffer1, 1023);
    //     printf("%s\n",(char*)buffer1);
    //     sleep(1);
    // }
    // sleep(100);

    // // 长连接测试1:只发送一次数据，然后休眠，使休眠时间>服务器超时关闭时间
    // Socket sock;
    // sock.CreateClient(38080, "127.0.0.1");
    // std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";

    // for(;;){
    //     ssize_t len = sock.Send(req.c_str(), req.size());
    //     char* buffer1[1024] = {0};
    //     sock.Recv(buffer1, 1023);
    //     printf("%s\n",(char*)buffer1);
    //     sleep(15);
    // }
    // sock.Close();

    // // 长连接测试3:
    // // 1.只发送一次数据，但在首行中标定正文长度>实际发送长度，使服务器进行等待接收数据，知道超时关闭连接
    // // 2.发送多次小数据，使服务器在处理完一个完整的数据之后，获取的小数据不在是固定的首行-头部-空行-正文的格式。（因为第二次小数据的信息被算在了第一个小数据的正文中）
    // Socket sock;
    // sock.CreateClient(38080, "127.0.0.1");
    // std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 50\r\n\r\nnihaoasdfasdfasdfasdf~~~";

    // for(;;){
    //     sock.Send(req.c_str(), req.size());
    //     sock.Send(req.c_str(), req.size());
    //     sock.Send(req.c_str(), req.size());
    //     sock.Send(req.c_str(), req.size());
    //     char* buffer1[1024] = {0};
    //     sock.Recv(buffer1, 1023);
    //     printf("%s\n",(char*)buffer1);
    //     sleep(15);
    // }
    // sock.Close();



    // for(int i=0;i<10;i++){
    //     pid_t pid = fork();
    //     if(pid<0){
    //         fatallog<<"fork error";
    //         return -1;
    //     }
    //     else if(pid==0){
    //         Socket sock;
    //         sock.CreateClient(38080, "127.0.0.1");
    //         std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    //         for(;;){
    //             sock.Send(req.c_str(), req.size());
    //             char* buffer1[1024] = {0};
    //             sock.Recv(buffer1, 1023);
    //             printf("%s\n",(char*)buffer1);
    //         }
    //         sock.Close();
    //         exit(0);
    //     }
    // }
    // while (1) {
    //     sleep(1);
    // }

    // // 一次发送多条请求测试
    // Socket sock;
    // sock.CreateClient(38080, "127.0.0.1");
    // std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    // req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    // req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    // req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";

    // for(;;){
    //     sock.Send(req.c_str(), req.size());
    //     char* buffer1[1024] = {0};
    //     sock.Recv(buffer1, 1023);
    //     printf("%s\n",(char*)buffer1);
    //     sleep(15);
    // }
    // sock.Close();

    // 发送大文件
    Socket sock;
    sock.CreateClient(38080, "127.0.0.1");
    std::string req = "PUT /1234.txt HTTP/1.1\r\nConnection: keep-alive\r\n";
    std::string body ;
    Util::ReadFile("/home/lyt/linux-study/HttpServer/src/test/hello.txt", &body);
    req += "Content-Length: " + std::to_string(body.size()) ;
    req += "\r\n\r\n";
    std::cout << req;
    sock.Send(req.c_str(), req.size());
    sock.Send(body.c_str(), body.size());
    char* buffer1[1024] = {0};
    sock.Recv(buffer1, 1023);
    printf("%s\n",(char*)buffer1);
    sleep(15);

    sock.Close();


    return 0;
}